package com.example.demo.controller;

import com.example.demo.model.UserScore;
import com.example.demo.service.ScoreService;
import com.example.demo.util.GeneralResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/scores")
    public GeneralResponse students() {
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(scoreService.getAllScores());
//        System.out.println("??????????");
        return response;
    }

    @PostMapping("/score/add")
    public GeneralResponse add(@RequestParam String name, @RequestParam String sex, @RequestParam String score) {
        UserScore userScore = new UserScore();
        userScore.setSex(sex);
        userScore.setName(name);
        userScore.setScore(score);

        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(scoreService.add(userScore));
        return response;
    }

    @PutMapping("/score/update")
    public GeneralResponse update(@RequestParam String name, @RequestParam String sex, @RequestParam String score,
                                  @RequestParam int id) {
        UserScore userScore = new UserScore();
        userScore.setId(id);
        userScore.setName(name);
        userScore.setSex(sex);
        userScore.setScore(score);

        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(scoreService.update(userScore));
        return response;
    }

    @PostMapping("/score/delete")
    public GeneralResponse delete(@RequestParam int id) {
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(scoreService.deleteById(id));
        return response;
    }

    @GetMapping("/score")
    public GeneralResponse studentById(@RequestParam int id) {
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        List<UserScore> result = new ArrayList<>();
        result.add(scoreService.getById(id));
        response.setData(result);
        return response;
    }
}
